﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using CRMBiz;
using Newtonsoft.Json;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for LastResult
    /// </summary>
    public class LastResult : BaseHandler
    {
        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        //user defined function
        #region private method

        public void LastResultSelect()
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);

                DataSet ds = LastResultHandler.GetLastResultList(pageIndex, pageSize);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void LastResultSelectAll()
        {
            try
            {
                DataSet ds = LastResultHandler.GetLastResultList();
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public int LastResultAdd(string ResultDescription)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = LastResultHandler.AddLastResult(ResultDescription);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public int LastResultEdit(int ResultID, string ResultDescription)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = LastResultHandler.EditLastResult(ResultID, ResultDescription);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public bool LastResultDelete(int ResultID)
        {
            bool flag = false;
            try
            {
                flag = LastResultHandler.DeleteLastResult(ResultID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

       
        public void GetFolderName(int appid)
        {
            try
            {
                context.Response.Write(ClientProfileHandler.GetFolderName(appid));
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public int ReorderLastResult(int resultId, int orderNo)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = LastResultHandler.ReorderResult(resultId, orderNo);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }
        #endregion
    }
}